package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.dao.BaseDao;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.CancelShoe;
import cn.com.ege.mvc.model.business.OrderShoe;
import cn.com.ege.mvc.model.business.Shoe;
import cn.com.ege.mvc.model.business.ShoeWarehouse;
import cn.com.ege.mvc.service.BaseService;
import cn.com.ege.mvc.service.impl.business.CancelShoeService;
import cn.com.ege.mvc.service.impl.business.OrderShoeService;
import cn.com.ege.mvc.service.impl.business.ShoeService;
import cn.com.ege.mvc.service.impl.business.ShoeWarehouseService;
import cn.com.ege.mvc.util.PageController;

public class CancelShoeAction extends BaseAction implements IAction,
		Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private CancelShoe cancelShoe = null;
	private CancelShoeService cancelShoeService = null;
	
	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		
		String order_shoe_id = request.getParameter("order_shoe_id");//订单鞋款的id
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				
				int totalRows = cancelShoeService.getRecordCount(cancelShoe);		
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<CancelShoe> resourceList = cancelShoeService.list(
						new CancelShoe(), this.pager.getPageStartRow(),
						pager.getPageSize(), "", true);
				
				List<String> product_id_list = new ArrayList<String>();// 在页面中显示相应的成品料号
				Shoe shoe = new Shoe();
				ShoeService shoeService = new ShoeService();
				for (CancelShoe cancelShoe : resourceList) {
					shoe.setId(cancelShoe.getShoe_id());
					product_id_list.add(shoeService.get(shoe).getProduct_id());
					}
				
				request.setAttribute("product_id_list", product_id_list);
				this.pager.setData(resourceList);
				
				return getReturnURL(LIST);
			}
			// 点了添加 add
			else if ("add".equals(act)) {
				OrderShoe orderShoe = new OrderShoe();
				OrderShoeService orderShoeService = new OrderShoeService();
				orderShoe.setId(Long.parseLong(order_shoe_id));
				orderShoe = orderShoeService.get(orderShoe);
				Shoe shoe = new Shoe();
				ShoeService shoeService = new ShoeService();
				shoe.setId(orderShoe.getShoe_id());
				shoe = shoeService.get(shoe);
				
				//以下计算同一订单内已开料的数量
			    Long[] cutted = new Long[8];
				cutted[0] = BaseDao.getSum("size_33", "cutting", "order_shoe_id="+order_shoe_id);
				cutted[1] = BaseDao.getSum("size_34", "cutting", "order_shoe_id="+order_shoe_id);
				cutted[2] = BaseDao.getSum("size_35", "cutting", "order_shoe_id="+order_shoe_id);
				cutted[3] = BaseDao.getSum("size_36", "cutting", "order_shoe_id="+order_shoe_id);
				cutted[4] = BaseDao.getSum("size_37", "cutting", "order_shoe_id="+order_shoe_id);
				cutted[5] = BaseDao.getSum("size_38", "cutting", "order_shoe_id="+order_shoe_id);
				cutted[6] = BaseDao.getSum("size_39", "cutting", "order_shoe_id="+order_shoe_id);
				cutted[7] = BaseDao.getSum("size_40", "cutting", "order_shoe_id="+order_shoe_id);
				
				request.setAttribute("order_shoe_id", orderShoe.getId());
			    request.setAttribute("cutted", cutted);
				request.setAttribute("shoe_id", shoe.getId());	
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {	
						
						OrderShoe orderShoe = new OrderShoe();
						OrderShoeService orderShoeService = new OrderShoeService();
						
						orderShoe.setId(cancelShoe.getOrder_shoe_id());				
						orderShoe = orderShoeService.get(orderShoe);
												
						ShoeWarehouse shoeWarehouse = new ShoeWarehouse();//对冲单的鞋款进行入库汇总操作
						ShoeWarehouseService shoeWarehouseService = new ShoeWarehouseService();
						shoeWarehouse.setShoe_id(cancelShoe.getShoe_id());
						shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
						
						cancelShoeService.save(cancelShoe);//保存对象,写入该鞋款冲单入库记录
						if(null == shoeWarehouse){//添加一种新的鞋款入仓库
							shoeWarehouse = new ShoeWarehouse();
							shoeWarehouse.setShoe_id(orderShoe.getShoe_id());// 初始化数据
							shoeWarehouse.setSize_33(0L);
							shoeWarehouse.setSize_34(0L);
							shoeWarehouse.setSize_35(0L);
							shoeWarehouse.setSize_36(0L);
							shoeWarehouse.setSize_37(0L);
							shoeWarehouse.setSize_38(0L);
							shoeWarehouse.setSize_39(0L);
							shoeWarehouse.setSize_40(0L);
							shoeWarehouseService.save(shoeWarehouse);
							shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
						}
						shoeWarehouse.setSize_33(shoeWarehouse.getSize_33() + cancelShoe.getSize_33());
						shoeWarehouse.setSize_34(shoeWarehouse.getSize_34() + cancelShoe.getSize_34());
						shoeWarehouse.setSize_35(shoeWarehouse.getSize_35() + cancelShoe.getSize_35());
						shoeWarehouse.setSize_36(shoeWarehouse.getSize_36() + cancelShoe.getSize_36());
						shoeWarehouse.setSize_37(shoeWarehouse.getSize_37() + cancelShoe.getSize_37());
						shoeWarehouse.setSize_38(shoeWarehouse.getSize_38() + cancelShoe.getSize_38());
						shoeWarehouse.setSize_39(shoeWarehouse.getSize_39() + cancelShoe.getSize_39());
						shoeWarehouse.setSize_40(shoeWarehouse.getSize_40() + cancelShoe.getSize_40());// 完成入库
						
						shoeWarehouseService.update(shoeWarehouse);//保存对象
						orderShoe.setTransact_status(1L);//更改交易状态
						orderShoeService.save(orderShoe);
						
						
						return getReturnURL(LIST);
				} catch (BusinessException e) {
				
					request.setAttribute("cancelShoe", cancelShoe);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String id : ids) {
					CancelShoe cancelShoe = new CancelShoe();
					//OrderShoe orderShoe = new OrderShoe();
					if (null != id && !("".equals(id))) {
						cancelShoe.setId(Long.parseLong(id));
						cancelShoeService.delete(cancelShoe);
					
					}
				}
				return getReturnURL("getList");
			}
		}
		addMessage("参数不正确!! act=" + act);
		return getReturnURL(ERROR);
	}


	public PageController getPager() {
		return pager;
	}


	public void setPager(PageController pager) {
		this.pager = pager;
	}


	public CancelShoe getCancelShoe() {
		return cancelShoe;
	}


	public void setCancelShoe(CancelShoe cancelShoe) {
		this.cancelShoe = cancelShoe;
	}


	public CancelShoeService getCancelShoeService() {
		return cancelShoeService;
	}


	public void setCancelShoeService(CancelShoeService cancelShoeService) {
		this.cancelShoeService = cancelShoeService;
	}

	

}
